package team02.client.core.test.question;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;

import team02.client.util.Pair;

import com.google.gwt.core.client.JsArrayInteger;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.user.client.rpc.IsSerializable;

public class Question implements IsSerializable {

	/* QUESTION CONSTANTS */
	//public static final Pattern QUESTION = Pattern.compile("Q\\(\\d+\\)");

	public static final String TYPE_SELECTION = "QS";
	public static final String TYPE_WRITE = "QW";

	public static final String QUESTION_FIGURE = "FG";

	//public static final Pattern QUESTION_SELECTION = Pattern.compile("S\\(\\d+\\)");
	//public static final Pattern QUESTION_WRITE = Pattern.compile("WR\\(\\d+\\)");

	public static final String QUESTION_LIMIT = "LM";
	public static final String QUESTION_TIME_OUT = "TM";
	public static final String QUESTION_LIMIT_TYPE_TRAP = "TRAP";
	public static final String QUESTION_LIMIT_TYPE_TRI = "TRI";
	public static final String QUESTION_LIMIT_TYPE_REC = "REC";

	//public static final Pattern QUESTION_ANSWER = Pattern.compile("AN\\(\\d+\\)");
	//public static final String QUESTION_SCORE = "SC";

	public static final String QUESTION_ANSWER_TYPE_KS = "KS";
	public static final String QUESTION_ANSWER_TYPE_KSA = "KSA";
	public static final String QUESTION_ANSWER_TYPE_KSO = "KSO";

	public static final String QUESTION_ANSWER_TYPE_KW = "KW";

	public static final String QUESTION_ANSWER_TYPE_KWA = "KWA";
	public static final String QUESTION_ANSWER_TYPE_KWAA = "KWAA";
	public static final String QUESTION_ANSWER_TYPE_KWAO = "KWAO";

	public static final String QUESTION_ANSWER_TYPE_KWP = "KWP";
	public static final String QUESTION_ANSWER_TYPE_KWPA = "KWPA";
	public static final String QUESTION_ANSWER_TYPE_KWPO = "KWPO";

	public static final String QUESTION_EXAMINER_INSERT = "INS";
	public static final String QUESTION_EXAMINER_INSERT_TYPE_COMMENT = "CINP";
	public static final String QUESTION_EXAMINER_INSERT_TYPE_CHECK = "CHK";
	public static final String QUESTION_EXAMINER_INSERT_TYPE_MARK = "VINP";

	/**
	 * 分からない
	 */
	public static final String QUESTION_TYPE_ANSWER_ANC = "ANC";

	int id;
	int questionNo;
	String type;//QS, QW
	int testId;

	String content;
	String contentPicture;//Q(1), FG, xxx
	boolean hasFigure;

	boolean hasExaminerInsert;
	private String examinerInsertType;

	ArrayList<QuestionS> selectionList;
	ArrayList<QuestionWR> writeList;
	ArrayList<QuestionSC> scoreList;
	ArrayList<QuestionAN> answerList;
	ArrayList<QuestionAN> questionANC;

	String timeStyle;
	private long timeOutDuration;
	private Pair limitPoints;
	private String limitStyle;

	//voi moi examinee
	ArrayList<Integer> examineeSelection;
	ArrayList<String> examineeWrite;
	long usedTime;
	boolean isSubmitted;
	private int score;
	private String insertContent;

	public Question() {
		selectionList = new ArrayList<QuestionS>();
		writeList = new ArrayList<QuestionWR>();
		scoreList = new ArrayList<QuestionSC>();
		answerList = new ArrayList<QuestionAN>();
		questionANC = new ArrayList<QuestionAN>();

		examineeSelection = new ArrayList<Integer>();
		examineeWrite = new ArrayList<String>();
	}

	public int addSelection(QuestionS selection) {
		for (QuestionS questionS : selectionList) {
			if (questionS.getSelectionNumber() == selection.getSelectionNumber()) return 1;//exist
		}

		selectionList.add(selection);
		return 0;
	}

	public QuestionS getSelection(int selectionNumber) {
		for (QuestionS questionS : selectionList) {
			if (questionS.getSelectionNumber() == selectionNumber) return questionS;
		}
		return null;
	}

	public int addWrite(QuestionWR questionWR) {
		for (QuestionWR questionWR2 : writeList) {
			if(questionWR2.getWriteNumber() == questionWR.getWriteNumber()) return 1;
		}
		writeList.add(questionWR);
		examineeWrite.add("");
		return 0;
	}

	public QuestionAN getAnswer(int answerNumber) {
		for (QuestionAN questionAN : answerList) {
			if (questionAN.getAnswerNumber() == answerNumber) return questionAN;
		}
		return null;
	}

	public void addAnswer(QuestionAN answer) {
		this.answerList.add(answer);
	}

	public void addScore(QuestionSC scoreObj) {
		this.scoreList.add(scoreObj);
	}

	public QuestionSC getScore(int scoreNumber) {
		return this.scoreList.get(scoreNumber - 1);
	}

	/* GETTER and SETTER */

	
	public String getTimeStyle() {
		return this.timeStyle;
	}

	public String getInsertContent() {
		return insertContent;
	}

	public void setInsertContent(String insertContent) {
		this.insertContent = insertContent;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getTestId() {
		return testId;
	}

	public void setTestId(int testId) {
		this.testId = testId;
	}

	public String getExaminerInsertType() {
		return examinerInsertType;
	}

	public ArrayList<QuestionAN> getAnswerList() {
		return answerList;
	}

	public ArrayList<Integer> getExamineeSelection() {
		return examineeSelection;
	}

	public void setExamineeSelection(ArrayList<Integer> examineeSelection) {
		this.examineeSelection = examineeSelection;
	}

	public ArrayList<String> getExamineeWrite() {
		return examineeWrite;
	}

	public void setExamineeWrite(ArrayList<String> examineeWrite) {
		this.examineeWrite = examineeWrite;
	}

	public long getUsedTime() {
		return usedTime;
	}

	public void setUsedTime(long usedTime) {
		this.usedTime = usedTime;
	}

	public void setContentPicture(String contentPicture) {
		this.contentPicture = contentPicture;
	}

	public void setTimeStyle(String timeStyle) {
		this.timeStyle = timeStyle;
	}

	public int getQuestionNo() {
		return this.questionNo;
	}

	public void setQuestionNo(int questionNo) {
		this.questionNo = questionNo;
	}

	public String getType() {
		return this.type;
	}

	public void setType(String questionType) {
		this.type = questionType;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContentPicture() {
		return this.contentPicture;
	}

	public void setFigure(String contentPicture) {
		this.contentPicture = contentPicture;
	}

	public ArrayList<QuestionS> getSelectionList() {
		return this.selectionList;
	}

	public void setSelectionList(ArrayList<QuestionS> selectionList) {
		this.selectionList = selectionList;
	}

	public ArrayList<QuestionWR> getWriteList() {
		return this.writeList;
	}

	public void setWriteList(ArrayList<QuestionWR> writeList) {
		this.writeList = writeList;
		for (int i = 0; i < writeList.size(); i++) {
			this.examineeWrite.add("");
		}
	}

	public ArrayList<QuestionSC> getScoreList() {
		return this.scoreList;
	}

	public void setScoreList(ArrayList<QuestionSC> scoreList) {
		this.scoreList = scoreList;
	}

	public void setHasFigure(boolean b) {
		this.hasFigure = b;
	}

	public boolean hasFigure() {
		return hasFigure;
	}

	public void setHasExaminerInsert(boolean b) {
		this.hasExaminerInsert = b;
	}

	public boolean hasExaminerInsert() {
		return this.hasExaminerInsert;
	}

	public ArrayList<QuestionAN> getQuestionANC() {
		return questionANC;
	}

	public void setQuestionANC(ArrayList<QuestionAN> questionANC) {
		this.questionANC = questionANC;
	}

	public void setTimeOutDuration(long time) {
		this.timeOutDuration = time;
	}

	public long getTimeOutDuration() {
		return this.timeOutDuration;
	}

	public void setLimitPoints(Pair limitPoints) {
		this.limitPoints = limitPoints;
		this.timeOutDuration = limitPoints.second;
	}

	public Pair getLimitPoints() {
		return this.limitPoints;
	}

	public void setLimitStyle(String limitStyle) {
		this.limitStyle = limitStyle;
	}

	public String getLimitStyle() {
		return this.limitStyle;
	}

	public void setExaminerInsertType(String string) {
		this.examinerInsertType = string;
	}

	public String getMarkerInsertType() {
		return this.examinerInsertType;
	}

	public long getTimeOutDuration1() {
		if (limitPoints != null) return limitPoints.first;
		return 0;
	}

	public void setAnswerList(ArrayList<QuestionAN> answerList) {
		this.answerList = answerList;
	}

	public boolean isSubmitted() {
		return isSubmitted;
	}

	public void setSubmitted(boolean isSubmitted) {
		this.isSubmitted = isSubmitted;
	}

	public void setExamineeSelection(JsArrayInteger selected) {
		this.examineeSelection = new ArrayList<Integer>(selected.length());
		for (int i = 0; i < selected.length(); i++) {
			this.examineeSelection.add(selected.get(i));
		}
	}

	public void setExamineeWrite(JsArrayString write) {
		this.examineeWrite = new ArrayList<String>(write.length());
		for (int i = 0; i < write.length(); i++) {
			this.examineeWrite.add(write.get(i));
		}
	}
	
	public ArrayList<String> getRealExamineeWrite(){
		ArrayList<String> realWrites = new ArrayList<String>();
		for (String write : examineeWrite) {
			if(write == null || write.trim().length() > 0) {
				realWrites.add(write.trim());
			}
		}
		return realWrites;
	}

	public void setScore(int questionScore) {
		this.score = questionScore;
	}
	
	public int getScore() {
		return this.score;
	}
	
	private int maxScore;

	public int getMaxScore() {
		return maxScore;
	}

	public void setMaxScore(int maxScore) {
		this.maxScore = maxScore;
	}
	
}
